<?php
/**
 * CUser class file.
 *
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @copyright Copyright(c) 2010 Lucas Vasconcelos
 * @license BSD License http://www.lucasvasconcelos.com/yap/license
 * @link http://www.lucasvasconcelos.com/yap/
 * @version $Id: CUser.php 19 2010-02-08 14:52:38Z contato@lucasvasconcelos.com $
 * @package yap.security
 */

Prado::using('System.Security.TDbUserManager');

/**
 * CUser class.
 *
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @version 1.0 $Revision: 19 $
 * @package yap.security
 */
abstract class CUser extends TDbUser
{
	public function validateUser($username, $password)
	{
		$user_record=$this->getUserRecord($username);
		return (null!==$user_record && $user_record->{$this->PasswordField}==$password);
	}

	public function createUser($username)
	{
		$class=get_class($this);
		if($username===null)
			return new $class($this->Manager);
		else
		{
			$user_record=$this->getUserRecord($username);
			if(null!==$user_record)
			{
				$user=new $class($this->Manager);
				$user->Name=$username;
				$user->IsGuest=false;
				return $user;
			}
		}
		return null;
	}
	
	public function getPasswordField()
	{
		return 'password';
	}
	
	abstract public function getUserRecord($username=null);
}